namespace Leey.Net.eTerm.Net
{
    using System;
    using System.Net.Security;
    using System.Runtime.CompilerServices;
    using System.Security.Cryptography.X509Certificates;

    public abstract class BaseSocket
    {
        private string m_Address = null;
        private long m_ConnectTimeout = 0x4e20L;
        private int m_Port = 0;

        public event ObjectHandler OnConnect;

        public event ObjectHandler OnDisconnect;

        public event ErrorHandler OnError;

        public event OnSocketDataHandler OnReceive;

        public event OnSocketDataHandler OnSend;

        public event RemoteCertificateValidationCallback OnValidateCertificate;

        public virtual void Connect()
        {
        }

        public virtual void Disconnect()
        {
        }

        protected void FireOnConnect()
        {
            if (this.OnConnect != null)
            {
                this.OnConnect(this);
            }
        }

        protected void FireOnDisconnect()
        {
            if (this.OnDisconnect != null)
            {
                this.OnDisconnect(this);
            }
        }

        protected void FireOnError(Exception ex)
        {
            if (this.OnError != null)
            {
                this.OnError(this, ex);
            }
        }

        protected void FireOnReceive(byte[] b, int length)
        {
            if (this.OnReceive != null)
            {
                this.OnReceive(this, b, length);
            }
        }

        protected void FireOnSend(byte[] b, int length)
        {
            if (this.OnSend != null)
            {
                this.OnSend(this, b, length);
            }
        }

        protected bool FireOnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            if (this.OnValidateCertificate != null)
            {
                return this.OnValidateCertificate(sender, certificate, chain, sslPolicyErrors);
            }
            return true;
        }

        public virtual void Send(byte[] bData)
        {
        }

        public virtual void Send(string data)
        {
        }

        public virtual void StartCompression()
        {
        }

        public virtual void StartTls()
        {
        }

        public string Address
        {
            get
            {
                return this.m_Address;
            }
            set
            {
                this.m_Address = value;
            }
        }

        public virtual bool Connected
        {
            get
            {
                return false;
            }
        }

        public virtual long ConnectTimeout
        {
            get
            {
                return this.m_ConnectTimeout;
            }
            set
            {
                this.m_ConnectTimeout = value;
            }
        }

        public int Port
        {
            get
            {
                return this.m_Port;
            }
            set
            {
                this.m_Port = value;
            }
        }

        public virtual bool SupportsStartTls
        {
            get
            {
                return false;
            }
        }

        public delegate void OnSocketCompressionDebugHandler(object sender, byte[] CompData, int CompCount, byte[] UncompData, int UncompCount);

        public delegate void OnSocketDataHandler(object sender, byte[] data, int count);
    }
}
